package com.zsk.shop.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zsk.shop.common.base.BaseResponse;
import com.zsk.shop.common.utils.ToolUtil;
import com.zsk.shop.entity.ShopGoodsSku;
import com.zsk.shop.mapper.ShopGoodsSkuMapper;
import com.zsk.shop.model.results.GopGoodsListSkuListResult;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author ljh
 * @create 2021/11/17 16:12
 */
@Service
public class ShopGoodsSkuService extends ServiceImpl<ShopGoodsSkuMapper, ShopGoodsSku> {

    public List<ShopGoodsSku> listByGoodsNos(List<String> goodsNos) {
        if (ToolUtil.isEmpty(goodsNos)) {
            return new ArrayList<>();
        }
        LambdaQueryWrapper<ShopGoodsSku> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.in(ShopGoodsSku::getGoodsNo, goodsNos);
        queryWrapper.eq(ShopGoodsSku::getIsDeleted, false);
        return list(queryWrapper);
    }

    public List<ShopGoodsSku> listByGoodsNo(String goodsNo) {
        LambdaQueryWrapper<ShopGoodsSku> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ShopGoodsSku::getGoodsNo, goodsNo);
        queryWrapper.eq(ShopGoodsSku::getIsDeleted, false);
        return list(queryWrapper);
    }

    /**
     * 获取商品sku信息
     *
     * @param bizNo
     * @return
     */
    public ShopGoodsSku getGoodsSkuInfo(String bizNo) {
        LambdaQueryWrapper<ShopGoodsSku> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.in(ShopGoodsSku::getBizNo, bizNo);
        return getOne(queryWrapper);
    }

    /**
     * 用户下单后更新库存
     *
     * @param bizNo
     * @param number
     */
    public void updateGoodsSku(String bizNo, Integer number) {
        this.baseMapper.updateGoodsSkuStock(bizNo, number);
    }

    /**
     * 增加库存
     *
     * @param bizNo
     * @param number
     */
    public void addGoodsSkuStock(String bizNo, Integer number) {
        this.baseMapper.addGoodsSkuStock(bizNo, number);
    }

    public BaseResponse updateGoodsSkuStock(List<GopGoodsListSkuListResult> list) {
        List<Long> ids = list.stream().map(GopGoodsListSkuListResult::getId).collect(Collectors.toList());
        List<ShopGoodsSku> shopGoodsSkuList = this.listBySkuIds(ids);
        for (ShopGoodsSku shopGoodsSku : shopGoodsSkuList) {
            GopGoodsListSkuListResult result = list.stream().filter(x -> x.getId().equals(shopGoodsSku.getId())).findFirst().orElse(null);
            if (ToolUtil.isNotEmpty(result)) {
                shopGoodsSku.setStock(result.getStock());
            }
        }
        this.saveOrUpdateBatch(shopGoodsSkuList);
        return BaseResponse.success("修改库存成功。");
    }

    private List<ShopGoodsSku> listBySkuIds(List<Long> ids) {
        LambdaQueryWrapper<ShopGoodsSku> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.in(ShopGoodsSku::getId, ids);
        return list(queryWrapper);
    }
}
